/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2022-09-26
 * Time: 20:53
 */
public class MyLinkedList {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val){
            this.val =val;
        }
    }
    public ListNode head;
    public ListNode tail;
    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public int size(){
        int count = 0;
        ListNode cur = head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void addFrist(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            this.tail = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null) {
            this.head = node;
            this.tail = node;
        }else{
            tail.next = node;
            node.prev = tail;
            tail = node;
        }
    }
    public boolean contain(int val){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == val){
                return true;
            }
            cur =cur.next;
        }
        return false;
    }
    public void add(int index,int data) throws indexWrongFulExpection {
        if(index < 0||index > size()){
            System.out.println("index的位置不合法");
            throw new indexWrongFulExpection("index的位置不合法");
        }
        if(index == 0){
            addFrist(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode cur = toFindindexnode(index);
        ListNode node = new ListNode(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;

    }
    private ListNode toFindindexnode(int index){
        ListNode cur = head;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    public void remove(int data) {
        ListNode cur = head;
        while (cur != null){
            if(cur.val == data){
                if(cur == head){
                    head = head.next;
                    if(head != null){
                        head.prev =null;
                    }else{
                        tail = null;
                    }
                }else{
                    cur.prev.next = cur.next;
                      if(cur == tail){
                         tail = cur.prev;
                       }else{
                          cur.next.prev = cur.prev;
                      }
                }
                return;
            }
            cur = cur.next;
        }
    }
    public void removeAll(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    if(head != null){
                        head.prev =null;
                    }else{
                        tail = null;
                    }
                }else{
                    cur.prev.next = cur.next;
                    if(cur == tail){
                        tail = cur.prev;
                    }else{
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    public void clear(){
        ListNode cur = head;
        while(cur == null){
            ListNode curNext = cur.next;
            cur.prev =null;
            cur.next = null;
        }
        head = null;
        tail = null;
    }
}
